package mashibing.class17;

import java.util.ArrayList;
import java.util.List;

/**
 * 打印一个字符串的全部排列，要求不要出现重复的排列
 */
public class Class17_4_PrintAllPermutations {

    public static List<String> permutation(String s) {
        char[] str = s.toCharArray();
        List<String> ans = new ArrayList<>();
        f(str, 0, ans);
        return ans;
    }

    public static void f(char[] str, int index, List<String> ans) {
        if (index == str.length) {
            ans.add(String.valueOf(str));
            return;
        }
        boolean[] visit = new boolean[256];
        for (int i = index; i < str.length; i++) {
            if(!visit[str[i]]) {
                visit[str[i]] = true;
                swap(str, index, i);
                f(str, index + 1, ans);
                swap(str, index, i);
            }
        }
    }

    public static void swap(char[] str, int index1, int index2) {
        char temp = str[index1];
        str[index1] = str[index2];
        str[index2] = temp;
    }

    public static void main(String[] args) {
//        List<String> list = permutation("abc");
        List<String> list = permutation("acc");
        for (String s : list) {
            System.out.println(s);
        }
    }

}
